package cn.itsource.org.service.impl;

import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Transient;
import javax.xml.crypto.Data;
import java.util.List;

/**
 * 部门表的业务层实现类
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    /**
     * 注入持久层依赖
     */
    @Autowired
    private DepartmentMapper mapper;

    /**
     * 查询所有的部门数据
     * @return 部门数据集合
     */
    @Override
    public List<Department> findAll() {
        return mapper.selectAll();
    }

    /**
     * 根据id查询一个部门数据
     * @param id 主键id
     * @return  部门对象
     */
    @Override
    public Department findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageList<Department> selectPage(DepartmentQuery query) {
        //1.传入分页参数
        PageHelper.startPage(query.getPage(),query.getRows());
        //2.调用通过模板类查询方法
        List<Department> departments = mapper.selectPage(query);
        //3.实例化pageinfo并传入参数
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        //4.封装自定义的分页对象
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        //5.返回分页信息
        return pageList;
    }

    /**
     * 新增或修改一个部门对象
     * 新增没有主键id  修改有主键id
     * @param department  主键id
     */
    @Override
    public void saveOrUpdate(Department department) {
        //判断id是否为空
        if (department.getId() == null){
            //如果id为空进行的是新增操作
            //mapper.insert(department);
            //只有设置了值的字段才会被新增进去
            mapper.insertSelective(department);

            //调用方法
            makeParentDept(department);

            //修改这个对象
            mapper.updateByPrimaryKey(department);
        }else {
            //如果id不为空进行的是修改操作
            //调用方法
            makeParentDept(department);
            //根据主键id修改,只有设置了值的字段才会被新增进去
            mapper.updateByPrimaryKeySelective(department);
        }
    }

    /**
     * ctrl + alt + m抽取共同的方法
     * 新增和修改共同的方法
     */
    private void makeParentDept(Department department) {
        Long[] parentIds = department.getParentIds(); // 3 8 10
        if (parentIds != null && parentIds.length > 0) {
            //1.设置parentId的值
            Long parentId = parentIds[parentIds.length - 1];
            department.setParentId(parentId);

            //2.循环 用快捷提示: 输入iter
            StringBuilder dirPath = new StringBuilder("");
            for (Long id : parentIds) {
                dirPath.append("/").append(id);
            }

            //3.加上自己的主键id值
            dirPath.append("/").append(department.getId());
            department.setDirPath(dirPath.toString());
        } else {
            department.setParentId(null);
            department.setDirPath("/" + department.getId());
        }
    }

    /**
     * 根据主键id删除一个部门对象
     * @param id 主键id
     */
    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     * @param ids
     */
//    @Override
//    public void batchDelete(Long[] ids) {
//        mapper.batchDelete(ids);
//    }

    /**
     * 查询上级部门
     * @return
     */
    @Override
    public List<Department> findParentDepts() {
        return mapper.findParentDepts();
    }

    /**
     * 选择上级部门
     * @return
     */
    @Override
    public List<Department> treeList() {
        //1.查询所有的工具类集合
        List<Department> departmentList = mapper.selectAll();
        //2.使用工具类的方法
        List<Department> list = TreeUtil.getTree(departmentList);
        //3.返回数据
        return list;
    }
}
